import { obj_float, obj_str } from 'sui';

const adjust = (d, k) => {
    const v = d[k];
    if (!v) {
        return;
    }

    let tmp = Math.abs(v);
    tmp = Math.floor(tmp * 10) % 10;

    if (tmp <= 5) {
        d[k] = Math.trunc(v);
    }
    else {
        d[k] = Math.round(v);
    }
};

const adjust_5 = (d, k) => {
    let v = obj_str(d, k);
    if (!v) {
        return;
    }

    v = obj_float(d, k);
    v = v.toFixed(5);
    v = v.padEnd(5, "0");
    v = v.slice(0, 5);

    d[k] = v;
};

const adjust_round = (d, k) => {
    const tmp = obj_str(d, k);
    if (!tmp) {
        return;
    }

    const v = obj_float(d, k);
    d[k] = Math.round(v);
};

const adjust_round_min = (d, k) => {
    const tmp = obj_str(d, k);
    if (!tmp) {
        return;
    }

    let v = obj_float(d, k);
    v = Math.round(v);

    if (v < 1) {
        v = 1;
    }

    d[k] = v;
};

const adjust_val = (r) => {
    r.forEach(d => {
        adjust(d, "outdia");
        adjust(d, "india");

        adjust_round(d, "outwidth");
        adjust_round(d, "standout");
        adjust_round(d, "dent");

        adjust_round_min(d, "kia");
        adjust_round_min(d, "sia");
        adjust_round_min(d, "kea");
        adjust_round_min(d, "sea");

        adjust_5(d, "angle");
    });
};

export {adjust_val};
